home *** CD-ROM | disk | FTP | other *** search
/ Aminet 38 / Aminet 38 (2000)(Schatztruhe)[!][Aug 2000].iso / Aminet / docs / mags / ripper11.lha / Ripper11 / text / programming tutorial < prev    next >
Encoding:
Text File  |  2000-06-12  |  6.7 KB  |  144 lines

  1. ^^                    James Rambles on about Programming
  2.                       ==================================
  3.  
  4.                            Part 1: C and functions
  5.  
  6. Hello world!  Welcome to this programming tutorial.  Now I'm sure you're all
  7. dying  to write program,  seeing as you are here and have got DICE  on  your
  8. hard drive,  so put this in your text editor,  and save it.  I'll assume you
  9. put all this example code in a directory called "Src:" and save it using the
  10. filenames I give them.  This is a C program (as the .c suffix suggests)  and
  11. we will be working in C for quite a while.
  12.  
  13.              --- Example 1.1: Hello, world! Filename: hello.c ---
  14.  
  15. #include <stdio.h>
  16.  
  17. int main(void)
  18. {
  19.   printf("Hello, world!\n");
  20.   /* This function prints "Hello, world!" and starts a new line */
  21.   return 0;
  22. }
  23.  
  24.  
  25.  
  26. Right, now you can compile this program, by opening a shell and typing:
  27.  
  28.   cd Src:       <enter>
  29.   dcc hello.c   <enter>
  30.  
  31. [One  way  of  opening a shell is to type newshell in the "Execute  Command"
  32. window, which you get from choosing that option from the Workbench pull-down
  33. menu.] This should make a program file Src:hello,  which you can execute  by
  34. typing hello <enter> in your shell. You should not need to enter the cd line
  35. more than once in your shell.
  36.  
  37.       Right let's see what has happened.  First, what does the code do:
  38.  
  39. "#include  <stdio.h>"  is  a   preprocessor   directive  --  this  means  it
  40. effectively  changes what your code says -- you can tell if something  is  a
  41. preprocessor  directive by seeing if it has a  hash  symbol  ('#')   at  the
  42. beginning.  The #include directive is an "insert file" option,  essentially,
  43. so  this  tells the compiler to add all the code from the file stdio.h  into
  44. your  program.  You need the stdio.h file whenever you use certain  features
  45. of C,  including printf.  The next line marks the beginning of a function. A
  46. function is a self- contained piece of code which takes in certain  numbers,
  47. does something,  then gives out another number.  "int main(void)" says  that
  48. the  function is called "main," that it doesn't take any numbers in ("void")
  49. and that it returns a number of type "int."
  50.  
  51. There  are  many  different kinds of numbers in C.  The type "int"  contains
  52. integers,   and  typically on the Amiga,  it  contains  numbers  from  0  to
  53. 4,294,967,295 or -2,147,483,648 to 2,147,483,647. "void" is also a type, but
  54. it cannot contain anything.
  55.  
  56. "main"  is a special function in C.  When a program is run from an  AmigaDOS
  57. shell  or Workbench (or whatever)  the function main is@1 "called,"  meaning
  58. that  the code contained in it is run.  This code may,  in turn,  call other
  59. functions, and so on. The numbers that can go into main (the parameters)
  60. are derived from the command line,  which was "hello" above, but can contain
  61. further text after the name of the program file.  In this example,  we chose
  62. to  ignore the rest of the command line,  and so used a void parameter.  The
  63. number sent out from main (the return value)  goes back to AmigaDOS, and can
  64. be used to indicate whether or not a program was successful.
  65.  
  66. "main"  consists of all the code between the pair of braces (curly brackets,
  67. '{'  and  '}' which follow it.  These braces act just like you would  expect
  68. brackets to,  in that if there was another open-brace ('{') within main, the
  69. end  of main would be marked by the second close-brace@1 ('}')  and not  the
  70. first.  There are two statements in our main. A statement is a piece of code
  71. which ends in a ";" symbol.
  72.  
  73. This means that, unlike in certain other languages, there is no restrictions
  74. on the way the code is laid out,  so statements may appear split over lines,
  75. and  many statements,  or part-statements may appear on one line.  The  main
  76. restrictions  are that a preprocessor directive must appear  on  a  line  by
  77. itself,  and that a piece of text in double- quotes ('"')  cannot be  simply
  78. split over two lines.  This means that the layout of my example code is just
  79. what looks nice to me,  and can be changed.  The "printf("Hello World!\n");"
  80. calls  the printf function with a parameter that represents the text  inside
  81. the quotes;  it prints this text to the shell window. In fact it sends it to
  82. a  device  known as stdout,  which is usually assigned to the shell  window,
  83. (although this can be altered in a number of ways).  However,  you will have
  84. noticed that you did not see "\n" in you shell window.  This is because '\n'
  85. is  a special symbol to represent a "new-line" character,  and it makes sure
  86. that  your  shell  prompt does not appear on the same line  as  your  "Hello
  87. World!" text.
  88.  
  89. The next line of my code contains only a comment.  A comment in C is started
  90. by the pair of characters "/*" and ended by the first "*/" and is equivalent
  91. to a single space in the code.  Finally,  there is a return statement.  This
  92. particular statement sends the value 0 back to AmigaDOS.  When  called  from
  93. main,   this  is  the same as calling a function called exit  with  whatever
  94. number  you  gave to return.  Thus,  in main,  "return 0;" is  the  same  as
  95. "exit(0);".   Whenever  exit  is called it leaves  your  program  and  gives
  96. AmigaDOS the value of its parameter. And now you know how it all works.
  97.  
  98.                                  Prototyping
  99.  
  100. There  is  one  more  idea we need before we can start  writing  our  own  C
  101. functions, and that is prototypes. Prototypes are used to tell your compiler
  102. what types of things a function returns and has as arguments before it  gets
  103. to the section where you actually write the code.  The file stdio.h contains
  104. the  prototype  for printf,  and that is why it is needed whenever  you  use
  105. printf.  main is special, and does not need a prototype, because main cannot
  106. be called, and there are only two different prototypes allowed for main.
  107.  
  108. Writing  prototypes is simple,  they are statements which just  contain  the
  109. same information as the beginning of the function.  Thus,  if you wanted  to
  110. call  our  main  above  hello   instead,    its   prototype  would  be  "int
  111. hello(void);" We'll finish up with an example of our own function.
  112.  
  113.          --- Example 2: Hello, I'm a function! Filename: hello2.c
  114. ---
  115.  
  116. #include <stdio.h>
  117. /* needed for printf */
  118.  
  119. void /* we don't need this function to return anything */
  120. hello(void); /* nor do we need to give it any parameters */
  121.  
  122. int main(void)
  123. {
  124.   hello();  /* call the hello function without any parameters */
  125.  
  126.   return 20;  /* this line should never be reached because
  127.         hello calls exit */
  128. }
  129.  
  130. void hello(void)
  131. {
  132.   printf("Hello, I'm a function!\n"); /* print as before */
  133.  
  134.   exit(0); /* leave the program here, don't go back to main */
  135. }
  136.  
  137. ------
  138.  
  139. Right now,  this probably seems like a lot of work for very little gain, but
  140. functions  are the key to writing good C,  and to the whole of AmigaOS.   In
  141. Part  2,  we'll look at how to find out the return value from main or  exit,
  142. and at more types.
  143.  
  144.